package server.server01;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * 返回响应协议
 */
public class Server02 {
    private ServerSocket serverCocket;
    public static void main(String[] args) {
        Server02 server = new Server02();
        server.start();
    }
//启动服务
    public void start(){
        try {
            serverCocket = new ServerSocket(8888);
            receive();
        } catch (IOException e) {
            System.out.println("服务器启动失败");
             e.printStackTrace();
        }
    }
//接受连接
    public void receive(){
        try {
            Socket client =  serverCocket.accept();
            System.out.println("一个客户端建立了连接");
            //获取请求协议
            InputStream is = client.getInputStream();
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas,0,len);
            System.out.println(requestInfo);

            StringBuilder content = new StringBuilder();
            content.append("<html>");
            content.append("<head>");
            content.append("<meta charset=\"UTF-8\">");
            content.append("<title>");
            content.append("服务器响应成功");
            content.append("</title>");
            content.append("</head>");
            content.append("<body>");
            content.append("shsxt server终于回来了。。。。");
            content.append("</body>");
            content.append("</html>");
            int size = content.toString().getBytes().length;//必须获取字节长度
            StringBuilder responsInfo = new StringBuilder();
            String blank = " ";
            String CRLF = "\r\n";//换行符
            //返回响应协议
            //1.响应行:HTTP/1.1 200 OK
            responsInfo.append("HTTP/1.1").append(blank);
            responsInfo.append("200").append(blank);
            responsInfo.append("OK").append(CRLF);
            //2.响应头（最后一行存在空行）
            /*
			 Date:Mon,31Dec209904:25:57GMT
			Server:shsxt Server/0.0.1;charset=GBK
			Content-type:text/html
			Content-length:39725426
			 */
            responsInfo.append("Date:").append(new Date()).append(CRLF);
            responsInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
            responsInfo.append("Content-type:text/html").append(CRLF);
            responsInfo.append("Content-length:").append(size).append(CRLF);
            responsInfo.append(CRLF);
            //3.正文
            responsInfo.append(content.toString());
            //写出到客户端
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            bw.write(responsInfo.toString());
            bw.flush();
        } catch (IOException e) {
            System.out.println("客户端错误");
            e.printStackTrace();
        }
    }
//停止服务
    public void stop(){

    }
}
